import axios from 'axios'
import store from '@/store' 
import router from '@/router'
// create an axios instance
const service = axios.create({
  // baseURL: 'http://chenyp.top:8008', // url = base url + request url
  baseURL: 'http://localhost:8080/', // url = base url + request url
  withCredentials: true, // send cookies when cross-domain requests
  timeout: 10000 ,// request timeout

})

// request interceptor
// 请求拦截器 
service.interceptors.request.use( 
  async config => {
    // 排除登录相关的请求 
    if (!config.url.includes('/login'))  {
      try {
        await store.dispatch('user/getLoginUser') 
      } catch (error) {
        // 处理错误 
      }
    }
    return config 
  },
  error => {
    return Promise.reject(error) 
  }
)
// response interceptor
service.interceptors.response.use(
  /**
   * If you want to get http information such as headers or status
   * Please return  response => response
  */

  /**
   * Determine the request status by custom code
   * Here is just an example
   * You can also judge the status by HTTP Status Code
   */
 
  response =>{
    return response.data
  }
)

export default service

